﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using NDF.Security.Cryptography;
using NDF.Utilities;
using System;
using System.Data;
using System.Linq;
using System.Web.Security;
using System.Security.Cryptography;
using System.IO;

namespace NDF.Tests.Framework.Security
{
    [TestClass]
    public class SymmetricAlgorithmsTest1
    {
        string plaintext = "ChenJianwei陈建伟<>:{}~!#@#%";
        string ciphertext = "uibngHAPNiq6N/WT53JwYpsMWjbe49ru4aB65OGyBrk=";

        //byte[] key = new byte[] { 167, 175, 27, 80, 186, 241, 29, 219, 71, 14, 37, 195, 157, 202, 193, 33, 58, 22, 56, 89, 73, 184, 224, 181, 147, 79, 247, 147, 83, 249, 142, 18 };
        //byte[] iv = new byte[] { 177, 68, 125, 107, 158, 22, 222, 94, 251, 28, 27, 214, 60, 92, 69, 27 };
        byte[] key = new byte[] { 228, 215, 91, 152, 247, 79, 150, 141 };
        byte[] iv = new byte[] { 224, 35, 23, 46, 93, 211, 9, 120 };


        [TestMethod]
        public void General()
        {
            var text1 = "陈建伟";
            var text2 = "陈建wei";
            var text3 = "chenjianwei";
            var text4 = "chenjian伟<>~!";

            //Console.WriteLine(BitConverter.ToString(HashAlgorithms.SHA1(System.Text.Encoding.Default.GetBytes(text))));
            //Console.WriteLine(BitConverter.ToString(HashAlgorithms.SHA1(System.Text.Encoding.Unicode.GetBytes(text))));
            //Console.WriteLine(BitConverter.ToString(HashAlgorithms.SHA1(System.Text.Encoding.ASCII.GetBytes(text))));
            //Console.WriteLine(BitConverter.ToString(HashAlgorithms.MD5(text1.ToBytes())));
            //Console.WriteLine(BitConverter.ToString(HashAlgorithms.MD5(text2.ToBytes())));
            //Console.WriteLine(BitConverter.ToString(HashAlgorithms.MD5(text3.ToBytes())));
            //Console.WriteLine(BitConverter.ToString(HashAlgorithms.MD5(text4.ToBytes())));

            Console.WriteLine(HashAlgorithms.MD516(text1));
            Console.WriteLine(HashAlgorithms.MD516(text2));
            Console.WriteLine(HashAlgorithms.MD516(text3));
            Console.WriteLine(HashAlgorithms.MD516(text4));

            //Console.WriteLine(HashAlgorithms.MD5(text4.ToBytes()).Length);


            //Console.WriteLine(BitConverter.ToString(SHA1.Create().ComputeHash(System.Text.Encoding.Default.GetBytes(text))));
            //Console.WriteLine(BitConverter.ToString(SHA1.Create().ComputeHash(System.Text.Encoding.Unicode.GetBytes(text))));
            //Console.WriteLine(BitConverter.ToString(SHA1.Create().ComputeHash(System.Text.Encoding.UTF32.GetBytes(text))));
            //Console.WriteLine(BitConverter.ToString(SHA1.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(text))));
            //Console.WriteLine(BitConverter.ToString(SHA1.Create().ComputeHash(System.Text.Encoding.UTF8.GetBytes(text))));


            //var algorithm = SymmetricAlgorithm.Create();

            //Console.WriteLine(algorithm.GetType());

            //Console.WriteLine(BitConverter.ToString(System.Text.Encoding.Default.GetBytes("chen建wei")));
            //Console.WriteLine(BitConverter.ToString(System.Text.Encoding.Unicode.GetBytes("chen建wei")));
            //Console.WriteLine(BitConverter.ToString(System.Text.Encoding.ASCII.GetBytes("chen建wei")));


            //Console.WriteLine(System.Text.Encoding.ASCII.GetString(System.Text.Encoding.Default.GetBytes("chen建wei")));
            //Console.WriteLine(System.Text.Encoding.ASCII.GetString(System.Text.Encoding.Unicode.GetBytes("chen建wei")));
            //Console.WriteLine(System.Text.Encoding.ASCII.GetString(System.Text.Encoding.ASCII.GetBytes("chen建wei")));

            //foreach (var item in System.Text.Encoding.GetEncodings())
            //{
            //    Console.WriteLine(item.DisplayName);
            //}
        }


        [TestMethod]
        public void GenerateKeyAndIV()
        {
            using (SymmetricAlgorithm algorithm = SymmetricAlgorithm.Create("DES"))
            {
                algorithm.GenerateKey();
                algorithm.GenerateIV();
                foreach (var b in algorithm.Key)
                    Console.Write(b + ", ");

                Console.WriteLine();

                foreach (var b in algorithm.IV)
                    Console.Write(b + ", ");
            }
        }


        [TestMethod]
        public void RijndaelEncryptTest()
        {
            using (SymmetricAlgorithm algorithm = SymmetricAlgorithm.Create("DES"))
            {
                algorithm.Key = key;
                algorithm.IV = iv;

                ICryptoTransform encryptor = algorithm.CreateEncryptor();

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(plaintext);
                        }
                        var bytes = msEncrypt.ToArray();
                        Console.WriteLine(Convert.ToBase64String(bytes));
                    }
                }
            }
        }

        [TestMethod]
        public void RijndaelDecryptTest()
        {
            using (SymmetricAlgorithm algorithm = SymmetricAlgorithm.Create("DES"))
            {
                algorithm.Key = key;
                algorithm.IV = iv;

                ICryptoTransform decryptor = algorithm.CreateDecryptor();

                var bytes = Convert.FromBase64String(ciphertext);
                string plaintext = null;

                using (MemoryStream msDecrypt = new MemoryStream(bytes))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader swDecrypt = new StreamReader(csDecrypt))
                        {
                            plaintext = swDecrypt.ReadToEnd();
                        }
                    }
                }
                Console.WriteLine(plaintext);
            }
        }

        [TestMethod]
        public void TestMethod1()
        {
            //string text = "ChenJianwei陈建伟<>:{}~!#@#%";
            //byte[] bytes = text.ToBytes();
            //Console.Write(System.Text.Encoding.UTF8.GetString(bytes));
            //Console.WriteLine(Aes.Create().GetType());
            //Console.WriteLine(DES.Create().GetType());
            //Console.WriteLine(RC2.Create().GetType());
            //Console.WriteLine(Rijndael.Create().GetType());
            //Console.WriteLine(TripleDES.Create().GetType());

            var aes = Aes.Create();
            aes.GenerateKey();
            aes.GenerateIV();
            Console.WriteLine(aes.Key.Length);
            Console.WriteLine(aes.IV.Length);
            Console.WriteLine(aes.KeySize);
            foreach (var k in aes.LegalKeySizes)
            {
                Console.WriteLine("{0}-{1}-{2}", k.MinSize, k.MaxSize, k.SkipSize);
            }
        }

        [TestMethod]
        public void CompareTest1()
        {
            //Console.WriteLine(SymmetricAlgorithms.CompareAES(plaintext, SymmetricAlgorithms.EncryptAES(plaintext, key, iv), key, iv));
            //Console.WriteLine(SymmetricAlgorithms.CompareDES(plaintext, SymmetricAlgorithms.EncryptDES(plaintext, key, iv), key, iv));
            var text = SymmetricAlgorithms.EncryptRC2(plaintext, key, iv);
            Console.WriteLine(text);
            Console.WriteLine(SymmetricAlgorithms.CompareRC2(plaintext, text, key, iv));
            //Console.WriteLine(SymmetricAlgorithms.CompareRijndael(plaintext, SymmetricAlgorithms.EncryptRijndael(plaintext, key, iv), key, iv));
            //Console.WriteLine(SymmetricAlgorithms.CompareTripleDES(plaintext, SymmetricAlgorithms.EncryptTripleDES(plaintext, key, iv), key, iv));
        }

        [TestMethod]
        public void EncryptTest1()
        {
            //string text = "ChenJianwei陈建伟<>:{}~!@#%";
            //string cipher = "zPqEFkvLKXGfzhc7ASqZOXqAFYmZiv96fXgHCFhJvrM=";
            //string key = "ChenJianwei";
            //string iv = "sbmy";

            //Console.WriteLine(SymmetricAlgorithms.CompareRijndael(text, cipher, key, iv));

            //Console.WriteLine(SymmetricAlgorithms.EncryptAES(text, key, iv));
            //Console.WriteLine(SymmetricAlgorithms.EncryptDES(text, key, iv));
            //Console.WriteLine(SymmetricAlgorithms.EncryptRC2(text, key, iv));
            //Console.WriteLine(SymmetricAlgorithms.EncryptRijndael(text, key, iv));
            //Console.WriteLine(SymmetricAlgorithms.EncryptTripleDES(text, key, iv));
            //Console.WriteLine(SymmetricAlgorithms.CompareAES(text, cipher, key, iv));

            Char c = '%';
            Console.WriteLine(Convert.ToByte(c));
        }

        [TestMethod]
        public void DecryptTest1()
        {

        }
    }
}
